﻿using System;
using System.ComponentModel ;
using System.Linq;
using LayerData.DataBaseManager;
using LayerEntity.Compras;
using Npgsql;

namespace LayerData.Compras
{
    public class DCompra
    {
        #region Propiedades
        public IPostgreSqlConnectionInfo CurrentConnection { get; set; }
        #endregion

        #region Constructor
        public DCompra(IPostgreSqlConnectionInfo con)
        {
            this.CurrentConnection = con;
        }

        public DCompra()
        {
            this.CurrentConnection = ObjectFactory.CreateConnectionInfo();
        }

        #endregion

        #region Funciones Publicas
        public int RegistraFactura(ECompra c)
        {
            int res = 0;
            NpgsqlConnection con = this.CurrentConnection.GetPostgreSqlConnection();
            try
            {
                this.CurrentConnection.OpenConnection(con);
                NpgsqlTransaction tran = this.CurrentConnection.GetTransaction(con);
                object obj = PostgreSqlHelper.ExecuteScalar(this.CurrentConnection, tran, "compras.usp_inserta_compra", c.ToObject());
                if (obj != null)
                {
                    res = Convert.ToInt32(obj);
                    if (res > 0)
                    {
                        BindingList <EDetalleCompra> det = c.GetDetalle();
                        if (det.Count > 0)
                        {
                            bool sucefull = true;
                            foreach (EDetalleCompra dc in det)
                            {
                                int infoDet = this.RegistraDetalle(tran, dc);
                                if (infoDet <= 0)
                                {
                                    sucefull = false;
                                    break;
                                }
                            }
                            if (!sucefull)
                            {
                                tran.Rollback();
                            }
                            else
                            {
                                this.CurrentConnection.CommintTransaction(tran);
                            }
                        }
                        else
                        {
                            tran.Rollback();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                this.CurrentConnection.CloseConnection(con);
            }

            return res;
        }

        private int RegistraDetalle(NpgsqlTransaction tran, EDetalleCompra dc)
        {
            int res = 0;
            try
            {
                object obj = PostgreSqlHelper.ExecuteScalar(this.CurrentConnection, tran, "compras.usp_inserta_detallecompra", dc.ToObject());
                if (obj != null)
                {
                    res = Convert.ToInt32(obj);
                }
            }
            catch (Exception ex)
            {
                DataBaseUtils.PrintMessajeError("Error: En Detalle de Compra", ex);
            }
            return res;
        }
        #endregion
    }
}